package com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.entity.StructureSetting;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.service.StructureSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/22
 * @Description: 结构类型设置
 */
@RestController
@RequestMapping("/structureSetting")
@Slf4j
public class StructureSettingController {
    @Resource
    private StructureSettingService structureSettingService;

    /***
     * 保存如果该省id有数据则修改,如果没有则保存
     * @param structureSetting
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody StructureSetting structureSetting){
        RestResponse restResponse=null;

        try {
            return structureSettingService.save(structureSetting);
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "保存失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 根据省id获取数据
     * @return ParamSetting
     */
    @GetMapping("/getStructureSetting")
    public RestResponse getStructureSetting(String id) {
        RestResponse restResponse = null;
        try {
            StructureSetting structureSetting=structureSettingService.getParamSetting(id);
            restResponse = RestResponse.succeed(structureSetting);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取登录人角色权限的省id
     * @return
     */
    @GetMapping("/getProvinceIdByUser")
    public RestResponse getProvinceIdByUser() {
        RestResponse restResponse = null;
        try {
            String provinceId=structureSettingService.getProvinceIdByUser();
            restResponse = RestResponse.succeed(provinceId);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(e.getMessage(), e);
            restResponse = RestResponse.fail(e.getMessage());
        }
        return restResponse;
    }

    /**
     * App端根据经纬度获取数据
     * @param lon 经度
     * @param lat 纬度
     * @return ParamSetting
     */
    @GetMapping("/getByLonAndLatOfApp")
    public RestResponse getByLonAndLatOfApp(String lon,String lat) {
        RestResponse restResponse = null;
        try {
            StructureSetting structureSetting=structureSettingService.getByLonAndLatOfApp(lon,lat);
            if (structureSetting==null){
                restResponse = RestResponse.fail("查询失败!");
            }else {
                restResponse = RestResponse.succeed(structureSetting);
            }
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
